var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '选择您的地址',
    phoneNumber: '短信验证',
    titleStyle: 'font-size:15px; color:#434343;font-weight:normal;',
    imgStyle: 'width:28rpx; height:34rpx;',
    phoneStyle: 'font-size:15px; color:#626262;font-weight:normal;',
    name:'',
    companyImg:'',
    licenseImg:'',
    coverImg:'',
    motto:'',
    content:'',
    introduce:'',
    latitude:'',
    longtitude:''
  },
  uploadCoverImg:function(event){
    console.log(event)
    var that = this
    wx.uploadFile({
      url: app.globalData.baseUrl + 'file/wechat',
      filePath: event.detail.path.tempFilePaths[0],
      name: 'files',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      formData: {
        'modular': 4
      },

      success: function (res) {
        console.log(res)
        var imgData = JSON.parse(res.data)
        that.setData({
          coverImg: imgData.data
        })
      }
    })
  },
  yanzhen:function(){
    wx.navigateTo({
      url: '/fabu/duanxin/duanxin',
    })
  },
  getName:function(event){
     this.setData({
       name:event.detail.value
     })
  },
  getMotto:function(event){
    this.setData({
      motto:event.detail.value
    })
  },
  getContent:function(event){
    this.setData({
      content:event.detail.value
    })
  },
  uploadImg:function(event){
    console.log(event)
    var that=this
    wx.uploadFile({
      url: app.globalData.baseUrl+'file/wechat',
      filePath: event.detail.path.tempFilePaths[0],
      name: 'files',
        header:{
        'content-type': 'application/x-www-form-urlencoded',
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },  
      formData:{
        'modular':4
      },
    
      success:function(res){
        console.log(res)
         var imgData=JSON.parse(res.data)
         that.setData({
           licenseImg:imgData.data
         })
      }
    })
  },
  uploadCompanyImg:function(event){
    var that=this
    wx.showModal({
      title: '',
      content: '最多上传5张图片',
      showCancel:false,
      confirmColor:'#02a0e9'
    })
    try{
      for (var index = 0; index < event.detail.path.tempFilePaths.length;index++) {
        wx.uploadFile({
          url: app.globalData.baseUrl + 'file/wechat',
          filePath: event.detail.path.tempFilePaths[index],
          name: 'files',
          formData: {
            'modular': 4
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
          },
          success: function (res) {
            console.log(res)
            var imgData = JSON.parse(res.data)
             var url='companyImg['+index+']'
            that.setData({
              [url]:imgData.data
            })
          }
        })
      }
    }catch(err){
      wx.showToast({
        title: '上传图片出错',
        icon:'none'
      })
      console.log(err)
    }
    
    
  },
  getIntroduce:function(event){
    
    this.setData({
      introduce:event.detail.value
    })
  },
  applyCompany:function(){
    var that=this
    var imgStr=that.data.companyImg.join(',')
    wx.request({
      url: app.globalData.baseUrl + 'mechanism/mechanism',
      method: "POST",
      data: {
        purpose: that.data.motto,
        introduce: that.data.introduce,
        img:that.data.coverImg,
        businessScope: that.data.content,
        position: that.data.address,
        latitude: that.data.latitude,
        longtitude: that.data.longtitude,
        name: that.data.name,
        telephone: wx.getStorageSync('telephone'),
        telephoneCode: wx.getStorageSync('checktxt'),
        license: that.data.licenseImg,
        imgList: imgStr
      },
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.msg == '请求成功') {
          wx.showModal({
            title: '',
            content: '申请成功请耐心等待审核',
            success: function (res) {
              wx.navigateBack({

              })
            }
          })
        }

      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  submit:function(event){
    var that=this
    if(this.data.name==''){
      wx.showToast({
        title: '请输入机构名称',
        icon:'none'
      })
    }else if(this.data.motto==''){
      wx.showToast({
        title: '请输入机构宗旨',
        icon: 'none'
      })
    } else if (this.data.content == '') {
      wx.showToast({
        title: '请输入机构涵盖内容',
        icon: 'none'
      })
    } else if (this.data.licenseImg == '') {
      wx.showToast({
        title: '请上传机构营业执照照片',
        icon: 'none'
      })
    } else if (this.data.introduce == '') {
      wx.showToast({
        title: '请输入机构简介',
        icon: 'none'
      })
    } else if (this.data.companyImg == '') {
      wx.showToast({
        title: '请上传机构照片',
        icon: 'none'
      })
    } else if (this.data.latitude == '') {
      wx.showToast({
        title: '请选择机构位置',
        icon: 'none'
      })
    } else if (wx.getStorageSync('checktxt') == '') {
      wx.showToast({
        title: '请填写短信验证码',
        icon: 'none'
      })
    }else{
      this.applyCompany()
    }
  },
  chooseAdd: function (event) {
    var that = this
    var location
    var add
    //这是一个异步函数
    wx.chooseLocation({
      success: function (res) {
        var add = res.address
        console.log(res)
        if (add.length > 20) {
          add = res.name
        }
        that.setData({
          address: add,
          latitude:res.latitude,
          longtitude:res.longitude
        })

      },
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var isApply = false
    try {
      isApply = options.isApply
    } catch (err) {

    }
    if (!isApply) {
      wx.request({
        url: app.globalData.baseUrl + 'personal/apply/mechanism',
        method: 'get',
        header: {
          'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
        },
        success: function (res) {
          console.log(res)
          try {

            var status = res.data.data.status
            if (status == 1 || status == 0  || status == 4) {

              wx.redirectTo({
                url: '/geren/apply/apply?status=' + status,
              })
            } else if (status == 2  ) {
              wx.showModal({
                title: '申请失败',
                content: '您提交的申请成为机构审核失败，可以重新提交申请'
              })
            } else if (status == 3){
              wx.showModal({
                title: '申请成功',
                content: '您提交的申请成为机构审核成功，可以在此页面来修改您的资料'
              })
            }


          } catch (err) {

          }



        },
        fail: function (err) {
          wx.showModal({
            title: '获取申请信息失败',
            content: '',
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})